load("@rules_oci//oci:defs.bzl", "oci_image")
load("@rules_pkg//:pkg.bzl", "pkg_tar")

genrule(
    name = "alpine-img",
    srcs = select({
        "@io_bazel_rules_go//go/platform:linux_arm64": ["@alpine_image_aarch64//file"],
        "@io_bazel_rules_go//go/platform:linux_s390x": ["@alpine_image_s390x//file"],
        "//conditions:default": ["@alpine_image//file"],
    }),
    outs = ["images/alpine/disk.img"],
    cmd = select({
        "@io_bazel_rules_go//go/platform:linux_arm64": "mkdir -p images/alpine && cat $(location @alpine_image_aarch64//file) > $@",
        "@io_bazel_rules_go//go/platform:linux_s390x": "mkdir -p images/alpine && cat $(location @alpine_image_s390x//file) > $@",
        "//conditions:default": "mkdir -p images/alpine && cat $(location @alpine_image//file) > $@",
    }),
)

genrule(
    name = "custom-img",
    srcs = [],
    outs = ["images/custom/disk.img"],
    cmd = "mkdir -p images/custom && truncate -s 64M disk.img && cat disk.img > $@",
)

pkg_tar(
    name = "alpine-tar",
    srcs = [
        ":alpine-img",
    ],
    package_dir = "images/alpine",
)

pkg_tar(
    name = "custom-tar",
    srcs = [
        ":custom-img",
    ],
    package_dir = "images/custom",
)

pkg_tar(
    name = "entrypoint-tar",
    srcs = [
        "entrypoint.sh",
    ],
    package_dir = "/",
)

oci_image(
    name = "disks-images-provider-image",
    base = "//images:kubevirt-testing-base",
    entrypoint = ["/entrypoint.sh"],
    tars = [
        ":entrypoint-tar",
        ":alpine-tar",
        ":custom-tar",
    ],
    visibility = ["//visibility:public"],
)
